<?php

require_once  __DIR__ . '/includes/authenticate.php';
include_once './db_bb_connect.php';

?>


<div id="viewtrainingshuttleruncontent">
<h4>View Shuttlerun Team Stats</h4>


<form>
	
<select class="form-control"  name="season" id="select3">
		
<?php

if (isset($_POST['season'])) {

	$setseason = $_POST['season'];

echo  "<option selected=\"selected\" value=\"$setseason\"> $setseason is selected</option>";
	
} else {
	echo  "<option selected=\"selected\" disabled=\"disabled\"> Select season</option>";
}


$sql = "SELECT DISTINCT season 
FROM trainingshuttlerun";

$result = $db->query($sql);

while ($row = $result->fetch_assoc()) {
	$season = $row['season'];

	echo "<option value=\"$season\"> $season </option>";
}

?>
</select>


</form>

<br>

<?php

if (isset($_POST['season'])) {
	$setseason = $_POST['season'];
		
	echo "<h4> Shuttlerun result of $setseason </h4>";

?>

<div class="table-responsive"> 
  <table class="table table-hover">
    <thead>
      <tr>
        <th>Playername</th>

<?php

$sql3 = "SELECT DISTINCT date 
FROM trainingshuttlerun 
ORDER BY date DESC";


$result3=$db->query($sql3);

while($row3=$result3->fetch_assoc()){ 

    $date = $row3['date']; 

    echo "<th>$date</th>";

}

?>


      </tr>
    </thead>
    <tbody>

<?php

$sql4 = "SELECT users.user_key, users.firstname, users.lastname 
FROM users
JOIN teams 
ON teams.playerID = users.user_key
WHERE teams.season = '$setseason'
ORDER BY users.playernumber
";

$sql6 = "SELECT DISTINCT trainingshuttlerun.playerID, users.firstname, users.lastname
  FROM trainingshuttlerun
  JOIN users 
  ON trainingshuttlerun.playerID = users.user_key
  WHERE trainingshuttlerun.season = '$setseason'
  ORDER BY trainingshuttlerun.date DESC";


$result4=$db->query($sql4);

while($row4=$result4->fetch_assoc()){ 

    $firstname = $row4['firstname'];
    $lastname = $row4['lastname'];
    $playerID =  $row4['user_key'];

    echo "<tr>";
    echo "<td>$firstname $lastname </td>";
    
    $result3=$db->query($sql3);

    while($row3=$result3->fetch_assoc()){ 

      $date = $row3['date']; 

      $sql5 = "SELECT trap 
        FROM trainingshuttlerun 
        WHERE playerID = '$playerID'
        AND date = '$date'";  

      $result5=$db->query($sql5);
      $row5=$result5->fetch_assoc();

      $trap = round($row5['trap'], 2);

      echo "<td>$trap</td>";

}

    echo "</tr> ";

}

?>
    </tbody>
  </table>
  </div>

</div>

<?php } ?>

<script type="text/javascript">
	
$("#select3").on("change", function(e) {
    postData = $(this.form).serialize();
    $.ajax({
        type: "POST",
        url: "./training_viewshuttlerunteamstats.php",
        data: postData,
        success: function(data) { 
        	//alert(postData);
            $("#viewtrainingshuttleruncontent").html(data); 
        },
        error: function (xhr, status, error) {
        	$("#viewtrainingshuttleruncontent").html(xhr.responseText); 
        }
      }); 
    e.preventDefault();
});  

$("#select2").on("change", function(e) {
    postData = $(this.form).serialize();
    $.ajax({
        type: "POST",
        url: "./training_viewshuttleteamstats.php",
        data: postData,
        success: function(data) { 
        	//(postData);
            $("#viewtrainingshuttleruncontent").html(data); 
        },
        error: function (xhr, status, error) {
        	$("#viewtrainingshuttleruncontent").html(xhr.responseText); 
        }
      }); 
    e.preventDefault();
});  

</script>